import axios from 'axios'
import { Toast } from 'vant'
// 基准路径
axios.defaults.baseURL = 'http://127.0.0.1:3000'
// axios默认不带cookie，需要设置让ajax携带cookie
axios.defaults.withCredentials = true

// 导航守卫
axios.interceptors.response.use(res => {
  // console.log(location.href, location.hash)
  if (res.data.error === 400) {
    Toast.fail(res.data.message)
    location.href = `/#/login?repath=/${location.hash}`
  }
  // console.log(res)
  return res
})

export const mobile = {
  // 分类页面查询top数据
  getTopCate: () => axios.get(`/category/queryTopCategory`),
  // 查询第二级数据
  getSecCate: obj => axios.get(`/category/querySecondCategory`, { params: obj }),

  // 登录页面登录
  login: obj => axios.post(`/user/login`, obj),

  // 搜索
  search: obj => axios.get(`/product/queryProduct`, { params: obj }),

  // 获取用户信息
  getUserInfo: () => axios.get(`/user/queryUserMessage`),

  // 获取商品详情
  getProductInfo: obj => axios.get(`/product/queryProductDetail`, { params: obj }),
  // 添加购物车
  addCart: obj => axios.post(`/cart/addCart`, obj),
  // 查询购物车
  queryCart: () => axios.get(`/cart/queryCart`),
  // 删除购物车
  delCart: obj => axios.get(`/cart/deleteCart`, { params: obj }),
  // 编辑更新购物车
  editCart: obj => axios.post(`/cart/updateCart`, obj)
}
